package com.nanyang.tms.manager.waybill.handler;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.nanyang.common.core.flow.context.TransHandlerContext;
import com.nanyang.common.core.flow.handler.AbstractTransHandler;
import com.nanyang.tms.common.util.TmsExceptionUtil;
import com.nanyang.tms.dao.forecast.ForecastDao;
import com.nanyang.tms.dao.receipt.ReceiptBillDao;
import com.nanyang.tms.dao.receipt.ReceiptRelationDao;
import com.nanyang.tms.domain.forecast.ForecastDetailVO;
import com.nanyang.tms.domain.forecast.context.CalculateWeightContext;
import com.nanyang.tms.domain.receipt.ReceiptBillDO;
import com.nanyang.tms.domain.receipt.ReceiptRelationDO;
import com.nanyang.tms.domain.waybill.context.SaveWaybillReceiptContext;
import com.nanyang.tms.domain.waybill.context.UpdateForecastContext;
import com.nanyang.tms.domain.waybill.context.UpdateReceiptContext;
@Component
public class UpdateReceiptHandler extends AbstractTransHandler {
    @Autowired
    private ReceiptBillDao receiptBillDao;
    @Autowired
    private ReceiptRelationDao receiptRelationDao;
    @Autowired
    private ForecastDao forecastDao;
	@Override
	public boolean isAsync() {
		return false;
	}

	@Override
	public boolean handle(TransHandlerContext context) {
	     CalculateWeightContext calculateWeightContext = (CalculateWeightContext) context.getInnerContext(CalculateWeightContext.class);
	     SaveWaybillReceiptContext saveWaybillReceiptContext = (SaveWaybillReceiptContext) context.getInnerContext(SaveWaybillReceiptContext.class);
	     try {
		     ForecastDetailVO forecastDetail = forecastDao.getForecastDetail(saveWaybillReceiptContext.getSupplierNo());
	    	 receiptBillDao.updateReceiptAccrual(bulidReceiptBill(saveWaybillReceiptContext,calculateWeightContext));
		     receiptRelationDao.insert(bulidReceiptRelation(forecastDetail,saveWaybillReceiptContext));
		     UpdateForecastContext updateForecastContext = (UpdateForecastContext) context.getInnerContext(UpdateForecastContext.class);
		     updateForecastContext.setForecastCode(saveWaybillReceiptContext.getForecastCode());
		     updateForecastContext.setState(1);
		} catch (Exception e) {
			throw TmsExceptionUtil.wrapException(e);
		}
		return true;
	}
	
	private ReceiptRelationDO bulidReceiptRelation(ForecastDetailVO forecastDetail,SaveWaybillReceiptContext saveWaybillReceiptContext){
		ReceiptRelationDO build = ReceiptRelationDO.builder()
		.receiptCode(saveWaybillReceiptContext.getReceiptCode())
		.customerNo(forecastDetail.getCustomerNo())
		.forecastCode(forecastDetail.getForecastCode())
		.saleProductCode(forecastDetail.getSaleProductCode())
		.saleProductName(forecastDetail.getSaleProductName())
		.supplierNo(saveWaybillReceiptContext.getSupplierNo())
		.waybillCode(saveWaybillReceiptContext.getWaybillCode())
		.build();
		 build.setIp(saveWaybillReceiptContext.getIp());
		 build.setGmtCreatePin(saveWaybillReceiptContext.getPin());
		 build.setGmtModifiedPin(saveWaybillReceiptContext.getPin());
		 return build;
	}
	private  ReceiptBillDO bulidReceiptBill(SaveWaybillReceiptContext saveWaybillReceiptContext, CalculateWeightContext calculateWeightContext){
		 ReceiptBillDO receiptBillDO=new ReceiptBillDO();
	     receiptBillDO.setReceiptCode(saveWaybillReceiptContext.getReceiptCode());
	     receiptBillDO.setTotalNum(saveWaybillReceiptContext.getTotalNum());
	     receiptBillDO.setTotalChargeWeight(calculateWeightContext.getTotalChargeWeight());
	     receiptBillDO.setTotalVolumeWeight(calculateWeightContext.getTotalVolumeWeight());
	     receiptBillDO.setTotalWeight(calculateWeightContext.getTotalWeight());
	     receiptBillDO.setReceiptTime(new Date());
	     receiptBillDO.setGmtModifiedPin(saveWaybillReceiptContext.getPin());
	     receiptBillDO.setGmtModified(new Date());
	     return receiptBillDO;
	}

}
